package keter.sec.springsec.security.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;


public class FailureHandler implements AuthenticationFailureHandler {
    /**
    * Logger for this class
    */
    private static final Logger logger = LoggerFactory.getLogger(FailureHandler.class);

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex)
            throws IOException, ServletException {
        String result = URLEncoder.encode("login?error=1&username="+ request.getParameter("username"), Charset.forName("UTF-8").toString());
//        response.sendRedirect("login?error=1&username="+ request.getParameter("username"));
        response.sendRedirect("login?error=1&username="+ result);
    }
}
	

